const isRequired = (required) => (required ? "" : "?");

export const genReqInterface = (data = {}) => {
  let res = [`/** ${data.title} 请求参数类型 */`, "export interface Root {"];

  (data.req_query || []).forEach((item) => {
    res.push(`  /** ${item.desc} */`);
    res.push(
      `  ${item.name}${isRequired(item.required === "1")}: StringOrNumber;`
    );
  });

  res.push("}");
  return res.join("\n");
};
const TYPE_MAP = {
  string: "string",
  integer: "number",
};

export const genResInterface = (data = {}) => {
  const dataSchema = JSON.parse(data.res_body).properties || {};

  let res = [`/** ${data.title} 响应参数类型 */`, "export interface Root {"];
  const requiredArr = dataSchema.data.required || [];
  Object.keys(dataSchema?.data?.properties ?? {}).forEach((key) => {
    const item = dataSchema.data.properties[key];
    res.push(`  /** ${item.description} */`);
    res.push(
      `  ${key}${isRequired(requiredArr.includes(key))}: ${
        TYPE_MAP[item.type]
      };`
    );
  });

  res.push("}");
  return res.join("\n");
};
